﻿@model SAIM.Models.TicketData
@using SAIM.Models.BLL
@using SAIM.Models
@using MvcFileUploader.HtmlHelper
@using MvcFileUploader.Models
@{
    SAIMIdentity UserIdentity = new SAIMIdentity(User.Identity);
    ViewBag.Title = "Detalles del ticket";
}
@section css {
    @Styles.Render("~/Content/bootstrap-select")
}
<div class="page-header">
    <h2>@ViewBag.Title</h2>
    <p class="text-success">@ViewBag.StatusMessage</p>
</div>

@using (Html.BeginForm(Model.Closed ? "OpenTicket" : "CloseTicket", "Tickets", FormMethod.Post, new { @class = "form-inline", role = "form", @style = "overflow-y: auto;" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m => m.IdTicket)
    <div class="form-group col-sm-12">
        <div class="panel panel-default table-responsive">
            <ul class="list-group col-md-6" style="padding-left:15px;">
                <li class="list-group-item list-group-item-text"><b>Información del ticket</b></li>
                <li class="list-group-item" style="border-bottom: none;">
                    <dl class="dl-horizontal">
                        <dt>Número de ticket:</dt>
                        <dd>@Model.IdTicket</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Área:</dt>
                        <dd>@Model.Area</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Servicio:</dt>
                        <dd>@Model.Issue</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Fecha y hora:</dt>
                        <dd>@Model.TicketDate</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Estatus:</dt>
                        <dd>@Model.Status</dd>
                    </dl>
                </li>
            </ul>
            <ul class="list-group col-md-6" style="padding-left:15px;">
                <li class="list-group-item list-group-item-text" style="border-top: none;"><b>Información del usuario</b></li>
                <li class="list-group-item">
                    <dl class="dl-horizontal">
                        <dt>Usuario:</dt>
                        <dd>@Model.UserName</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>UPN:</dt>
                        <dd>@Model.RPE</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Correo electrónico:</dt>
                        <dd>@Model.Email</dd>
                    </dl>
                    <dl class="dl-horizontal">
                        <dt>Perfil:</dt>
                        <dd>@Model.Profile</dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>
}

<div class="panel-group">
    <ul class="list-group">
        <li class="list-group-item" style="border:0px;">
            <div class="panel panel-default">
                <div class="panel-heading" style="border-bottom: 1px solid #ddd;">
                    <h4 class="panel-title">
                        <span class="text-primary">Otras opciones</span>
                    </h4>
                </div>
                <div class="panel-body">
                    <form class="form-inline">
                        <div class="form-group" style="width:100%;">
                            <div class="col-md-4">
                                <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>&nbsp;&nbsp;<a href="@Url.Action("Attachments", "Tickets", new { Id = Model.IdTicket })">Ver archivos adjuntos</a>
                            </div>
                            <div class="col-md-4">
                                <span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;&nbsp;<a href="@Url.Action("Users", "Tickets", new { Id = Model.IdTicket })">Usuarios colaboradores</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </li>
    </ul>
</div>

<div class="panel-group">
    <div class="panel-body">
        <ul class="list-group">
            <li class="list-group-item" style="background-color: #f5f5f5;"><span class="text-primary" style="font-size: 1.1em;">Comentarios</span></li>
            <li class="list-group-item">
                <form class="form-horizontal">
                    <div class="form-group">
                        <div class="col-sm-12">
                            <h4 class="list-group-item-heading">@Model.UserName</h4>
                            <h5 class="list-group-item-heading">@Model.TicketDate</h5>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <p class="list-group-item-text">@MvcHtmlString.Create(Model.Details.Replace(Environment.NewLine, "<br />"))</p>
                        </div>
                    </div>
                </form>
            </li>
            @foreach (TicketComment element in Model.Comments)
            {
                if (!element.IsPrivate)
                {
                    <li class="list-group-item">
                        @using (Html.BeginForm("EditComment", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                        {
                            @Html.Hidden("IdTicket", element.IdTicket)
                            @Html.Hidden("IdComment", element.IdComment)
                            <div class="form-group">
                                <div class="col-sm-8">
                                    <h4 class="list-group-item-heading">@element.UserName</h4>
                                    <h5 class="list-group-item-heading">@element.CommentDate</h5>
                                </div>
                                <div class="col-sm-4">
                                    <div class="btn-toolbar" style="float: right;" role="toolbar" aria-label="...">
                                        @if (!Model.Closed && (UserIdentity.GetUserId() == element.UserId) && element.CanEdit)
                                        {
                                            @Html.AntiForgeryToken()
                                            <div class="AddComment btn-group btn-group-sm invisible" role="group" aria-label="...">
                                                <button type="button" class="btn btn-default" title="Aceptar">
                                                    <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                                                </button>
                                            </div>
                                            <div class="CancelComment btn-group btn-group-sm invisible" role="group" aria-label="...">
                                                <button type="button" class="btn btn-default" title="Cancelar">
                                                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                                </button>
                                            </div>
                                            <div class="EditComment btn-group btn-group-sm" role="group" aria-label="...">
                                                <button type="button" class="btn btn-default" title="Editar">
                                                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                                </button>
                                            </div>
                                        }
                                        @if (element.EditionsCount > 0)
                                        {
                                            <div class="CommentHistory btn-group btn-group-sm" role="group" aria-label="...">
                                                <button type="button" class="btn btn-default" title="Historial de ediciones">
                                                    <span class="glyphicon glyphicon-time" aria-hidden="true"></span>
                                                </button>
                                            </div>
                                        }
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="comment-area col-sm-12">
                                    <p class="list-group-item-text">@MvcHtmlString.Create(element.Comment.Replace(Environment.NewLine, "<br />"))</p>
                                    @if (!Model.Closed)
                                    {
                                        @Html.TextAreaFor(m => m.Comment, new { @class = "form-control comment-textArea hide", @placeholder = "Comentario...", @rows = "2", @style = "resize: none;" })
                                        @Html.ValidationMessageFor(m => m.Comment, "", new { @class = "text-danger comment-validation hide" })
                                    }
                                </div>
                            </div>
                        }
                    </li>
                }
                else
                {
                    using (AspNetUsersBLL user = new AspNetUsersBLL(UserIdentity.GetUserId()))
                    {
                        if (user.isInRole("Manage tickets") && user.IsInArea("Mantenimiento"))
                        {
                            <li class="list-group-item">
                                @using (Html.BeginForm("EditComment", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                                {
                                    @Html.Hidden("IdTicket", element.IdTicket)
                                    @Html.Hidden("IdComment", element.IdComment)
                                    <div class="form-group">
                                        <div class="col-sm-8">
                                            <h4 class="list-group-item-heading">@element.UserName</h4>
                                            <h5 class="list-group-item-heading">@element.CommentDate</h5>
                                            <h5 class="list-group-item-heading">
                                                <span class="label label-info">
                                                    <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                                                </span>
                                                Comentario Administrativo
                                            </h5>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="btn-toolbar" style="float: right;" role="toolbar" aria-label="...">
                                                @if (!Model.Closed && (UserIdentity.GetUserId() == element.UserId))
                                                {
                                                    @Html.AntiForgeryToken()
                                                    <div class="AddComment btn-group btn-group-sm invisible" role="group" aria-label="...">
                                                        <button type="button" class="btn btn-default" title="Aceptar">
                                                            <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                                                        </button>
                                                    </div>
                                                    <div class="CancelComment btn-group btn-group-sm invisible" role="group" aria-label="...">
                                                        <button type="button" class="btn btn-default" title="Cancelar">
                                                            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                                        </button>
                                                    </div>
                                                    <div class="EditComment btn-group btn-group-sm" role="group" aria-label="...">
                                                        <button type="button" class="btn btn-default" title="Editar">
                                                            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                                        </button>
                                                    </div>
                                                }
                                                @if (element.EditionsCount > 0)
                                                {
                                                    <div class="CommentHistory btn-group btn-group-sm" role="group" aria-label="...">
                                                        <button type="button" class="btn btn-default" title="Historial de ediciones">
                                                            <span class="glyphicon glyphicon-time" aria-hidden="true"></span>
                                                        </button>
                                                    </div>
                                                }
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="comment-area col-sm-12">
                                            <p class="list-group-item-text">@MvcHtmlString.Create(element.Comment.Replace(Environment.NewLine, "<br />"))</p>
                                            @if (!Model.Closed)
                                            {
                                                @Html.TextAreaFor(m => m.Comment, new { @class = "form-control comment-textArea hide", @placeholder = "Comentario...", @rows = "2", @style = "resize: none;" })
                                                @Html.ValidationMessageFor(m => m.Comment, "", new { @class = "text-danger comment-validation hide" })
                                            }
                                        </div>
                                    </div>
                                }
                            </li>
                        }
                    }
                }
            }
        </ul>
        @using (AspNetUsersBLL user = new AspNetUsersBLL(UserIdentity.GetUserId()))
        {
            if (!Model.Closed || (user.isInRole("Manage tickets") && user.IsInArea("Mantenimiento")))
            { 
                using (Html.BeginForm("AddComment", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form", @id = "CommentForm" }))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-group">
                        <div class="col-sm-12">
                            @Html.ValidationMessageFor(m => m.Comment, "", new { @class = "text-danger" })
                        </div>
                        <div class="col-sm-12">
                            @Html.HiddenFor(m => m.IdTicket)
                            @Html.HiddenFor(m => m.IsPrivate)
                            @Html.HiddenFor(m => m.ToggleStatus)
                            @Html.HiddenFor(m => m.HasAttachedFiles)
                            @Html.TextAreaFor(m => m.Comment, new { @class = "form-control", @placeholder = "Comentario...", @style = "resize: none;", @rows = "2" })
                        </div>
                    </div>
                }
                <div class="row">
                    <div id="div-files" class="col-sm-7">
                        @(
                        Html.MvcFileUpload()
                        .UploadAt(Url.Action("UploadCommentFiles"))
                        .WithFileTypes(@"/(\.|\/)(gif|jpe?g|tiff|png|pdf|docx?|xlsx?|xml|txt)$/i")
                        .WithMaxFileSize(10000000)
                        .AddFormField("Id", Model.IdTicket.ToString())
                        .DisableImagePreview()
                        .ReturnAt("DetailsTicket")
                        .RenderInline("_MvcFileupload")
                        )
                    </div>
                    <div class="col-sm-5">
                        @if (user.isInRole("Manage tickets") && user.IsInArea("TIC"))
                        {
                            <div class="btn-group pull-right" role="group" aria-label="...">
                                <button type="button" class="btn btn-primary btn-comment-action">Comentar</button>
                                <select id="tipoComentario" class="selectpicker" data-width="auto">
                                    @if (!Model.Closed)
                                    {
                                        <option>Público</option>
                                        <option data-icon="glyphicon-user">Administrativo</option>
                                        <option data-icon="glyphicon-remove">Cerrar ticket</option>
                                    }
                                    else
                                    {
                                        <option data-icon="glyphicon-ok">Reabrir ticket</option>
                                    }
                                </select>
                            </div>
                        }
                        else
                        {
                            <div class="btn-group pull-right" role="group" aria-label="...">
                                <button type="button" class="btn btn-primary btn-comment-action">Comentar</button>
                                <div style="float:left; padding: 0px 4px 4px 5px;">
                                    <button type="button" class="btn btn-danger" id="cerrar-ticket">Cerrar Ticket</button>
                                </div>
                                <select id="tipoComentario" class="selectpicker hidden" data-width="auto">
                                    @if (!Model.Closed)
                                    {
                                        <option>Nuevo comentario</option>
                                        <option data-icon="glyphicon-remove">Cerrar ticket</option>
                                    }
                                </select>
                            </div>
                        }
                    </div>
                </div>
            }
        }
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" data-url="@Url.Action("_GetEditionsByComment", "Tickets")"></div> 

@section Scripts {
@Scripts.Render("~/bundles/bootstrap-select")
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript" src="@Url.Content("~/Scripts/Tickets/DetailsTicket.js")"></script>
}
